﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;

public partial class HTML_getQuestionLibrary : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.DataTable table1 = new System.Data.DataTable();
        try
        {
            //填充数据
            string sql = "select top 500 student_id,student_name from tb_student";

            //int  page =Int32.Parse(Request.QueryString["page"].ToString().Trim());
            //int limit = Int32.Parse(Request.QueryString["limit"].ToString().Trim());
            //int page_temp = page;
            //int limit_temp = limit;
            //int count = 500;
            //if (count < page * limit)
            //{
            //    limit = count - (page - 1) * limit;
            //}
            //page = (page_temp - 1) * limit_temp;
            //string sql = "select top 10 student_id,student_name from tb_student";
            table1 = SqlServer.GetDataTable(sql);
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('有错误')</script>");
            Response.End();
        }
        Response.Write(ToJson(table1));
        Response.End();

    }
    public static string ToJson(DataTable dt)
    {
        StringBuilder json = new StringBuilder();
        json.Append("[");
        DataRowCollection drc = dt.Rows;
        for (int i = 0; i < drc.Count; i++)
        {
            json.Append("{");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                string key = dt.Columns[j].ColumnName;

                string value = drc[i][j].ToString();
                Type type = dt.Columns[j].DataType;
                json.Append("\"" + key + "\":");
                value = StringFormat(value, type);
                json.Append(value);
                if (j < dt.Columns.Count - 1)
                {
                    json.Append(",");
                }
            }
            json.Append("},");
        }
        json.Remove(json.Length - 1, 1);
        json.Append("]");
        return "{\"code\":0 ," + "\"msg\": \"\"," + "\"count\":" + "500" + " , \"data\":" + json.ToString() + "}";
        //return json.ToString();
        //return "{\"code\":0 ,"+"\"content\": {\"currentPage\": 1,\"pageSize\": 3},"+ "\"count\":" + "500" + " , \"data\":" + json.ToString() + "}";

    }

    private static string StringFormat(string str, Type type)
    {
        if (type == typeof(string))
        {
            str = StringJson(str);
            str = "\"" + str + "\"";
        }
        else if (type == typeof(DateTime))
        {
            str = "\"" + str + "\"";
        }
        else if (type == typeof(bool))
        {
            str = str.ToLower();
        }
        else if (type != typeof(string) && string.IsNullOrEmpty(str))
        {
            str = "\"" + str + "\"";
        }
        return str;
    }
    /// <summary>
    /// 特殊字符处理
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    private static string StringJson(String s)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
        {
            char c = s.ToCharArray()[i];
            switch (c)
            {
                case '\"':
                    sb.Append("\\\""); break;
                case '\\':
                    sb.Append("\\\\"); break;
                case '/':
                    sb.Append("\\/"); break;
                case '\b':
                    sb.Append("\\b"); break;
                case '\f':
                    sb.Append("\\f"); break;
                case '\n':
                    sb.Append("\\n"); break;
                case '\r':
                    sb.Append("\\r"); break;
                case '\t':
                    sb.Append("\\t"); break;
                default:
                    sb.Append(c); break;
            }
        }
        return sb.ToString();
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }


}